package com.taxi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taxi.constant.DriverCarConstants;
import com.taxi.dto.Car;
import com.taxi.dto.DriverCarBindingRelationship;
import com.taxi.dto.DriverUser;
import com.taxi.mapper.CarMapper;
import com.taxi.mapper.DriverCarBindingRelationshipMapper;
import com.taxi.mapper.DriverUserMapper;
import com.taxi.result.Result;
import com.taxi.service.IDriverCarBindingRelationshipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aniu
 * @since 2023-09-28
 */
@Service
public class DriverCarBindingRelationshipServiceImpl extends ServiceImpl<DriverCarBindingRelationshipMapper, DriverCarBindingRelationship> implements IDriverCarBindingRelationshipService {
    @Autowired
    private CarMapper carMapper;

    @Autowired
    private DriverUserMapper driverUserMapper;

    @Override
    public Result bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        driverCarBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
        //判断:如果参数中的车辆和司机 已经绑定过 则不允许再次绑定
        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_id",driverCarBindingRelationship.getDriverId());
        wrapper.eq("car_id",driverCarBindingRelationship.getCarId());
        wrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND);

        //同一个司机不能重复绑定
        Long aLong = baseMapper.selectCount(wrapper);
        if(aLong.intValue()>0){
            return Result.fail("请勿重复绑定");
        }
        wrapper.clear();

        //司机被绑定了
        wrapper.eq("driver_id",driverCarBindingRelationship.getDriverId());
        wrapper.eq("bind_state",driverCarBindingRelationship.getBindState());
        aLong = baseMapper.selectCount(wrapper);
        if(aLong.intValue()>0){
            return Result.fail("该司机早已绑定过车辆");
        }
        wrapper.clear();

        //车辆被绑定了
        wrapper.eq("bind_state",driverCarBindingRelationship.getBindState());
        wrapper.eq("car_id",driverCarBindingRelationship.getCarId());
        aLong = baseMapper.selectCount(wrapper);
        if(aLong.intValue()>0){
            return Result.fail("该车辆早已被绑定了");
        }
        wrapper.clear();

        LocalDateTime now=LocalDateTime.now();
        driverCarBindingRelationship.setBindingTime(now);
        //顺带绑定车牌号
        Car car = carMapper.selectOne(new QueryWrapper<Car>().eq("id", driverCarBindingRelationship.getCarId()));
        driverCarBindingRelationship.setVehicleNo(car.getVehicleNo());
        baseMapper.insert(driverCarBindingRelationship);
        return Result.ok();
    }

    @Override
    public Result unbind(DriverCarBindingRelationship driverCarBindingRelationship) {
        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_id",driverCarBindingRelationship.getDriverId());
        wrapper.eq("car_id",driverCarBindingRelationship.getCarId());
        //必须是绑定的，才能解绑
        wrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship carBindingRelationship = baseMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(carBindingRelationship)) {
            return Result.fail("绑定关系不存在");
        }

        carBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_UNBIND);
        baseMapper.updateById(carBindingRelationship);
        return Result.ok("解绑成功");
    }

    public Result<DriverCarBindingRelationship> getDriverCarRelationShipByDriverPhone(String driverPhone){
        QueryWrapper<DriverUser> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_phone",driverPhone);
        DriverUser driverUser = driverUserMapper.selectOne(wrapper);
        Long driverId = driverUser.getId();

        QueryWrapper<DriverCarBindingRelationship> bindingRelationshipQueryWrapper = new QueryWrapper<>();
        bindingRelationshipQueryWrapper.eq("driver_id",driverId);
        bindingRelationshipQueryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship driverCarBindingRelationship = baseMapper.selectOne(bindingRelationshipQueryWrapper);
        return Result.ok(driverCarBindingRelationship);
    }
}
